using UnityEngine;
using UnityEditor;
using Devil;
using System.Collections.Generic;

namespace GameToolkit.Editor
{
    [CustomPropertyDrawer(typeof(SortingLayerFieldAttribute))]
    [CustomPropertyDrawer(typeof(LayerFieldAttribute))]
    public class SortingLayerFieldDrawer : PropertyDrawer
    {
        //StringBuilder builder = new StringBuilder();
        static string[] mSortingLayers;
        static int[] mSortingLayerIds;

        static string[] mLayerNames;
        static int[] mLayerIndex;

        [InitializeOnLoadMethodParallel(IsOnMainThread = true)]
        static void InitLayers()
        {
            SortingLayer[] layers = SortingLayer.layers;
            mSortingLayers = new string[layers.Length];
            mSortingLayerIds = new int[layers.Length];
            for (int i = 0; i < layers.Length; i++)
            {
                mSortingLayers[i] = layers[i].name;
                mSortingLayerIds[i] = layers[i].id;
            }
            var layerNames = new List<string>();
            var layerIndex = new List<int>();
            for (int i = 0; i < 32; i++)
            {
                var lname = LayerMask.LayerToName(i);
                if (!string.IsNullOrEmpty(lname))
                {
                    layerNames.Add(lname);
                    layerIndex.Add(i);
                }
            }
            mLayerNames = layerNames.ToArray();
            mLayerIndex = layerIndex.ToArray();
        }

        int GetSortingIndex(int id)
        {
            if (mSortingLayerIds == null)
                return -1;
            for (int i = 0; i < mSortingLayerIds.Length; i++)
            {
                if (mSortingLayerIds[i] == id)
                    return i;
            }
            return -1;
        }

        int GetLayerIndex(int id)
        {
            if (id == -1 || mLayerIndex == null)
                return -1;
            for (int i = 0; i < mLayerIndex.Length; i++)
            {
                if (mLayerIndex[i] == id)
                    return i;
            }
            return -1;
        }

        //void GetSortingLayers()
        //{
        //    int len = mSortingLayers == null ? 0 : mSortingLayers.Length;
        //    SortingLayer[] layers = SortingLayer.layers;
        //    if (layers.Length != len)
        //    {
        //        mSortingLayers = new string[layers.Length];
        //        mSortingLayerIds = new int[layers.Length];
        //    }
        //    for (int i = 0; i < layers.Length; i++)
        //    {
        //        mSortingLayers[i] = layers[i].name;
        //        mSortingLayerIds[i] = layers[i].id;
        //    }
        //}

        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            EditorGUI.BeginProperty(position, label, property);

            int lv = EditorGUI.indentLevel;
            position = EditorGUI.PrefixLabel(position, label);
            if (property.propertyType == SerializedPropertyType.Integer)
            {
                //GetSortingLayers();
                if (attribute is SortingLayerFieldAttribute)
                {
                    var size = mSortingLayers == null ? 0 : mSortingLayers.Length;
                    var id = GetSortingIndex(property.intValue);
                    int index = EditorGUI.Popup(position, id, mSortingLayers);
                    if (index != id && index >= 0 && index < size)
                    {
                        property.intValue = mSortingLayerIds[index];
                        GUI.changed = true;
                    }
                }
                else if (attribute is LayerFieldAttribute)
                {
                    var id = GetLayerIndex(property.intValue);
                    var size = mLayerIndex == null ? 0 : mLayerIndex.Length;
                    var index = EditorGUI.Popup(position, id, mLayerNames);
                    if (index != id && index >= 0 && index < size)
                    {
                        property.intValue = mLayerIndex[index];
                        GUI.changed = true;
                    }
                }
            }
            else
            {
                EditorGUI.LabelField(position, "Use Integer Instead of " + property.propertyType);
            }
            EditorGUI.indentLevel = lv;

            EditorGUI.EndProperty();
        }
    }
}